package com.java110.mall.shop.cmd.shopHouse;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.annotation.Java110Cmd;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.Cmd;
import com.java110.core.event.cmd.CmdEvent;
import com.java110.dto.shop.ShopHouseDto;
import com.java110.intf.shop.IShopHouseInnerServiceSMO;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.utils.util.StringUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * 查询仓库
 */
@Java110Cmd(serviceCode = "shopHouse.queryShopHouse")
public class QueryShopHouseCmd extends Cmd {

    @Autowired
    private IShopHouseInnerServiceSMO shopHouseInnerServiceSMOImpl;

    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
        super.validatePageInfo(reqJson);
    }

    @Override
    public void doCmd(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
        ShopHouseDto shopHouseDto = BeanConvertUtil.covertBean(reqJson, ShopHouseDto.class);
        int count = 0;
        ShopHouseDto shopHouse = new ShopHouseDto();
        shopHouse.setShopId(shopHouseDto.getShopId());
        shopHouse.setPage(shopHouseDto.getPage());
        shopHouse.setRow(shopHouseDto.getRow());
        if (!StringUtil.isEmpty(shopHouseDto.getFlag()) && shopHouseDto.getFlag().equals("1")) {
            count = shopHouseInnerServiceSMOImpl.queryShopHousesCount(shopHouse);
        } else {
            count = shopHouseInnerServiceSMOImpl.queryShopHousesCount(shopHouseDto);
        }
        List<ShopHouseDto> shopHouseDtos = new ArrayList<>();
        if (count > 0) {
            if (!StringUtil.isEmpty(shopHouseDto.getFlag()) && shopHouseDto.getFlag().equals("1")) {
                List<ShopHouseDto> shopHouseList = shopHouseInnerServiceSMOImpl.queryShopHouses(shopHouse);
                for (ShopHouseDto shopHouDto : shopHouseList) {
                    if (!StringUtil.isEmpty(shopHouDto.getShId()) && shopHouDto.getShId().equals(shopHouseDto.getShId())) {
                        continue;
                    } else {
                        shopHouseDtos.add(shopHouDto);
                    }
                }
            } else {
                shopHouseDtos = shopHouseInnerServiceSMOImpl.queryShopHouses(shopHouseDto);
            }
        } else {
            shopHouseDtos = new ArrayList<>();
        }

        ResultVo resultVo = new ResultVo((int) Math.ceil((double) count / (double) shopHouseDto.getRow()), count, shopHouseDtos);

        ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK);
        context.setResponseEntity(responseEntity);
    }
}
